package processor;

import java.util.Date;

import play.db.jpa.JPA;
import play.libs.F.Callback0;
import prefix.util.JPAUtil;

import models.Addr;
import models.Employment;
import models.Employment.State;
import repository.AbstractRepository;
import repository.AddrRepository;
import service.exception.EmploymentException;
import service.exception.EmploymentException.CODE;
import service.impl.EmploymentServiceImpl;

public class EmploymentProcessor extends EmploymentServiceImpl {

    public EmploymentProcessor(Employment employment) {
        super(employment);
    }

    public EmploymentProcessor() {
    }

    public void saveEmploymentInfo(Integer aid, Employment.Status employmentStatus, String companyName,
            String jobTitle, int jobTenureYears, String streetAddress, String city, String website, String province,
            Employment.CompanySegment companySegment, String companyPhone, String companyPhoneExt,
            Employment.CompanyType companyType, Employment.CompanySize companySize, Date establishDate,
            Double applicantShare, String orgCode, String regNO, Double optIncome, Double optCost, Double profit,
            State loanState, State mortgageState, State lawsuitState, State paidTaxState) throws EmploymentException {
        try {
            JPAUtil.beginTransaction();
            this.dbemployment.setValues(aid, employmentStatus, companyName, jobTitle, jobTenureYears, website,
                    companySegment, companyType, companySize, establishDate, applicantShare, orgCode, regNO, optIncome,
                    optCost, profit, loanState, mortgageState, lawsuitState, paidTaxState);
            Addr addr = new Addr();
            addr.setValues(aid, this.dbemployment.getId(), streetAddress, city, province, companyPhone, null, null,
                    companyPhoneExt);
            JPAUtil.em().persist(this.dbemployment);
            JPAUtil.em().persist(addr);
            JPAUtil.commitTransaction();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            throw new EmploymentException(CODE.UNKNOWN, e);
        } finally {
            JPAUtil.closeEM();
        }

    }

    public void updateEmploymentInfo(Integer aid, Employment.Status employmentStatus, String companyName,
            String jobTitle, int jobTenureYears, String streetAddress, String city, String website, String province,
            Employment.CompanySegment companySegment, String companyPhone, String companyPhoneExt,
            Employment.CompanyType companyType, Employment.CompanySize companySize, Date establishDate,
            Double applicantShare, String orgCode, String regNO, Double optIncome, Double optCost, Double profit,
            State loanState, State mortgageState, State lawsuitState, State paidTaxState) throws Throwable {
        try {
            JPAUtil.beginTransaction();
            this.dbemployment.setValues(aid, employmentStatus, companyName, jobTitle, jobTenureYears, website,
                    companySegment, companyType, companySize, establishDate, applicantShare, orgCode, regNO, optIncome,
                    optCost, profit, loanState, mortgageState, lawsuitState, paidTaxState);
            Addr addr = null;
            addr = AbstractRepository.getSingleByProperty(Addr.class, "employmentId", this.dbemployment.getId(), null);
            if (addr != null && addr.getId() != null) {
                addr.setValues(aid, this.dbemployment.getId(), streetAddress, city, province, companyPhone, null, null,
                        companyPhoneExt);
            } else {
                addr = new Addr();
                addr.setValues(aid, this.dbemployment.getId(), streetAddress, city, province, companyPhone, null, null,
                        companyPhoneExt);
                JPAUtil.em().persist(addr);
            }
            JPAUtil.commitTransaction();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            throw new EmploymentException(CODE.UNKNOWN, e);
        } finally {
            JPAUtil.closeEM();
        }

    }

}
